// pages/personal/about-card/about-card.js
var app=getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    picUrls: [],
    picIds: [],
    textContext: "",
    basicInfoId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      basicInfoId: options.personalId
    });
    console.log("接受到的personalId为:"+this.data.basicInfoId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //点击完成,提交表单
  aboutsubmit: function (e) {
    var data = e.detail.value;
    console.log(data);
    var url = app.globalData.serverAddress + "/about/save/art?_t=" + new Date().getTime();
    app.httpPost(url, data).then((res) => {
      if (res.data.flag) {
        console.log("添加成功");
        wx.navigateTo({
          url: "/pages/personal/more-card/more-card?cardId=" + res.data.cardId +"&_t=" + new Date().getTime(),
        })
      } else {
        console.log("失败!");
      }
    }).catch((errMsg) => {
      console.log(errMsg); //错误提示信息
      wx.showModal({
        content: '网络异常',
        showCancel: false,
      })
    });
  },
  /**
   * 点击选择图片并添加
   */
  about_img_btn: function () {
    var that = this;
    wx.chooseImage({
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      //图片选择成功的事件
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        var uploadUrl = app.globalData.serverAddress + '/api/company/upload?_t=' + new Date().getTime();
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (res.tempFiles[i].size <= 3145728) {
            wx.uploadFile({
              url: uploadUrl,
              filePath: tempFilePaths[i],
              name: "img",
              header: {
                "Content-Type": "multipart/form-data",
                'accept': 'application/json'
              },
              formData: {
                'user': 'test' //其他额外的formdata，可不写
              },
              //成功时执行函数
              success: function (res) {
                var result = JSON.parse(res.data);
                if (result.flag) {
                  that.data.picUrls.push(result.url);
                  that.data.picIds.push(result.picId);
                  that.setData({
                    picUrls: that.data.picUrls,
                    picIds: that.data.picIds
                  })
                } else {
                  console.log(result.msg);
                }
              },
              //失败时执行函数
              fail: function (res) {
                wx.showModal({
                  content: '网络异常，图片上传失败',
                  showCancel: false,
                  success: function (res) { }
                })
              },
              //不管成功或失败都会执行
              complete: function () {
                wx.hideLoading();
              }
            })
          } else {
            wx.showToast({
              title: '上传图片不能大于3M!',
              icon: 'none'
            })
          }
        }
      },
      //图片选择失败的事件
      fail: e => {
        console.error(e)
      }
    })
  }
})